/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapHelper;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapDomain;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapEntry;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapMultipleEntriesMatchedException;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZSearchControls;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEnumeration;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapHelper
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static LdapProv prov;
/*     */   private static LdapHelper ldapHelper;
/*     */   private static ZLdapFilterFactory filterFactory;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  64 */     provUtil = new LdapProvTestUtil();
/*  65 */     prov = provUtil.getProv();
/*  66 */     ldapHelper = provUtil.getProv().getHelper();
/*  67 */     filterFactory = ZLdapFilterFactory.getInstance();
/*  68 */     domain = provUtil.createDomain(baseDomainName(), null);
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  73 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchForEntry() throws Exception {
/*  78 */     LdapDIT dit = prov.getDIT();
/*  79 */     String base = dit.configBranchBaseDN();
/*  80 */     ZLdapFilter filter = filterFactory.fromFilterString(ZLdapFilterFactory.FilterId.UNITTEST, "(cn=config)");
/*     */     
/*  82 */     ZSearchResultEntry sr = ldapHelper.searchForEntry(base, filter, null, false);
/*     */     
/*  84 */     Assert.assertNotNull(sr);
/*  85 */     Assert.assertEquals("cn=config,cn=zimbra", sr.getDN());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchForEntryMultipleMatchedEntries() throws Exception {
/*  90 */     LdapDIT dit = prov.getDIT();
/*  91 */     String base = dit.configBranchBaseDN();
/*  92 */     ZLdapFilter filter = filterFactory.allAccounts();
/*     */     
/*  94 */     boolean caughtException = false;
/*     */     try {
/*  96 */       ZSearchResultEntry entry = ldapHelper.searchForEntry(base, filter, null, false);
/*     */       
/*  98 */       Assert.assertNotNull(entry);
/*     */     } catch (LdapException.LdapMultipleEntriesMatchedException e) {
/* 100 */       caughtException = true;
/*     */     }
/* 102 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchForEntryNotFound() throws Exception {
/* 107 */     LdapDIT dit = prov.getDIT();
/* 108 */     String base = dit.configBranchBaseDN();
/* 109 */     ZLdapFilter filter = filterFactory.fromFilterString(ZLdapFilterFactory.FilterId.UNITTEST, "(cn=bogus)");
/*     */     
/*     */ 
/* 112 */     ZSearchResultEntry sr = ldapHelper.searchForEntry(base, filter, null, false);
/*     */     
/* 114 */     Assert.assertNull(sr);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAttributes() throws Exception {
/* 119 */     String dn = prov.getDIT().configDN();
/* 120 */     ZAttributes attrs = ldapHelper.getAttributes(LdapUsage.UNITTEST, dn);
/* 121 */     Assert.assertEquals("config", attrs.getAttrString("cn"));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAttributesEntryNotFound() throws Exception {
/* 126 */     String dn = prov.getDIT().configDN() + "-not";
/*     */     
/* 128 */     boolean caughtException = false;
/*     */     try {
/* 130 */       attrs = ldapHelper.getAttributes(LdapUsage.UNITTEST, dn);
/*     */     } catch (LdapException.LdapEntryNotFoundException e) {
/*     */       ZAttributes attrs;
/* 133 */       caughtException = true;
/*     */     }
/* 135 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchDir() throws Exception {
/* 140 */     LdapDIT dit = prov.getDIT();
/* 141 */     String base = dit.configBranchBaseDN();
/* 142 */     ZLdapFilter filter = filterFactory.anyEntry();
/* 143 */     String[] returnAttrs = { "objectClass" };
/*     */     
/* 145 */     ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_ONELEVEL, 0, returnAttrs);
/*     */     
/*     */ 
/*     */ 
/* 149 */     ZSearchResultEnumeration ne = ldapHelper.searchDir(base, filter, searchControls);
/*     */     
/* 151 */     Set<String> expected = new HashSet();
/*     */     
/* 153 */     expected.add(dit.adminBaseDN());
/* 154 */     expected.add(dit.appAdminBaseDN());
/* 155 */     expected.add(dit.zimletBaseDN());
/* 156 */     expected.add(dit.cosBaseDN());
/* 157 */     expected.add(dit.globalDynamicGroupBaseDN());
/* 158 */     expected.add(dit.serverBaseDN());
/* 159 */     expected.add(dit.xmppcomponentBaseDN());
/* 160 */     expected.add(dit.globalGrantDN());
/* 161 */     expected.add(dit.configDN());
/* 162 */     expected.add(dit.shareLocatorBaseDN());
/* 163 */     expected.add(dit.ucServiceBaseDN());
/*     */     
/* 165 */     int numFound = 0;
/* 166 */     while (ne.hasMore()) {
/* 167 */       ZSearchResultEntry sr = ne.next();
/* 168 */       Assert.assertTrue(expected.contains(sr.getDN()));
/* 169 */       numFound++;
/*     */     }
/* 171 */     ne.close();
/*     */     
/* 173 */     Assert.assertEquals(expected.size(), numFound);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchDirNotFound() throws Exception {
/* 178 */     LdapDIT dit = prov.getDIT();
/* 179 */     String base = dit.configBranchBaseDN();
/* 180 */     ZLdapFilter filter = filterFactory.allSignatures();
/* 181 */     String[] returnAttrs = { "objectClass" };
/*     */     
/* 183 */     ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, 0, returnAttrs);
/*     */     
/*     */ 
/*     */ 
/* 187 */     ZSearchResultEnumeration ne = ldapHelper.searchDir(base, filter, searchControls);
/*     */     
/*     */ 
/* 190 */     int numFound = 0;
/* 191 */     while (ne.hasMore()) {
/* 192 */       ZSearchResultEntry sr = ne.next();
/* 193 */       numFound++;
/*     */     }
/* 195 */     ne.close();
/*     */     
/* 197 */     Assert.assertEquals(0L, numFound);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchDirSizeLimitExceeded() throws Exception {
/* 202 */     int SIZE_LIMIT = 5;
/*     */     
/* 204 */     String base = "";
/* 205 */     ZLdapFilter filter = filterFactory.anyEntry();
/* 206 */     String[] returnAttrs = { "objectClass" };
/*     */     
/* 208 */     ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, SIZE_LIMIT, returnAttrs);
/*     */     
/*     */ 
/*     */ 
/* 212 */     int numFound = 0;
/* 213 */     boolean caughtException = false;
/*     */     try {
/* 215 */       ZSearchResultEnumeration ne = ldapHelper.searchDir(base, filter, searchControls);
/* 216 */       while (ne.hasMore()) {
/* 217 */         ZSearchResultEntry sr = ne.next();
/* 218 */         numFound++;
/*     */       }
/* 220 */       ne.close();
/*     */     }
/*     */     catch (LdapException.LdapSizeLimitExceededException e) {
/* 223 */       caughtException = true;
/*     */     }
/* 225 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testAndModifyAttributes()
/*     */     throws Exception
/*     */   {
/* 238 */     ZLdapFilter filter = filterFactory.domainLockedForEagerAutoProvision();
/* 239 */     Map<String, Object> attrs = new HashMap();
/* 240 */     attrs.put("zimbraAutoProvLock", "blah");
/*     */     
/* 242 */     ZLdapContext zlc = null;
/*     */     try {
/* 244 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UNITTEST);
/* 245 */       ldapHelper.testAndModifyEntry(zlc, ((LdapEntry)domain).getDN(), filter, attrs, domain);
/*     */     }
/*     */     finally {
/* 248 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void hasSubordinates() throws Exception {
/* 254 */     Domain domain = provUtil.createDomain(genDomainName(baseDomainName()));
/* 255 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain);
/* 256 */     Group group = provUtil.createGroup(genGroupNameLocalPart(), domain, true);
/*     */     
/* 258 */     String domainDn = ((LdapDomain)domain).getDN();
/* 259 */     String acctBaseDn = prov.getDIT().domainDNToAccountBaseDN(domainDn);
/* 260 */     String dynGroupsBaseDn = prov.getDIT().domainDNToDynamicGroupsBaseDN(domainDn);
/*     */     
/* 262 */     ZLdapContext zlc = null;
/*     */     try {
/* 264 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UNITTEST);
/*     */       
/*     */ 
/* 267 */       Assert.assertTrue(hasSubordinates(zlc, acctBaseDn));
/* 268 */       Assert.assertTrue(hasSubordinates(zlc, dynGroupsBaseDn));
/*     */       
/* 270 */       provUtil.deleteAccount(acct);
/* 271 */       provUtil.deleteGroup(group);
/*     */       
/*     */ 
/* 274 */       Assert.assertFalse(hasSubordinates(zlc, acctBaseDn));
/* 275 */       Assert.assertFalse(hasSubordinates(zlc, dynGroupsBaseDn));
/*     */     }
/*     */     finally {
/* 278 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean hasSubordinates(ZLdapContext zlc, String dn) throws Exception {
/* 283 */     boolean hasSubordinates = false;
/*     */     
/* 285 */     ZSearchResultEnumeration ne = null;
/*     */     try {
/* 287 */       ne = ldapHelper.searchDir(dn, filterFactory.hasSubordinates(), ZSearchControls.SEARCH_CTLS_SUBTREE(), zlc, LdapServerType.MASTER);
/*     */       
/* 289 */       hasSubordinates = ne.hasMore();
/*     */       
/* 291 */       if (hasSubordinates) {
/* 292 */         int numEntries = 0;
/* 293 */         String entryDn = null;
/* 294 */         while (ne.hasMore()) {
/* 295 */           ZSearchResultEntry sr = ne.next();
/* 296 */           entryDn = sr.getDN();
/* 297 */           numEntries++;
/*     */         }
/* 299 */         Assert.assertEquals(1L, numEntries);
/* 300 */         Assert.assertEquals(dn, entryDn);
/*     */       }
/*     */     } finally {
/* 303 */       if (ne != null) {
/* 304 */         ne.close();
/*     */       }
/*     */     }
/*     */     
/* 308 */     return hasSubordinates;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapHelper.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */